//
//  ShangChengVC.m
//  digitalCurrency
//
//  Created by 小震GG on 2019/10/30.
//  Copyright © 2019 XinHuoKeJi. All rights reserved.
//

#import "ShangChengVC.h"
#import "SDCycleScrollView.h"
#import "NewAllNetManager.h"
#import "KuangJiVC.h"
#define Start_X 15.0f           // 第一个按钮的X坐标
#define Start_Y 50.0f           // 第一个按钮的Y坐标
#define Width_Space 0.0f        // 2个按钮之间的横间距
#define Height_Space 0.0f      // 竖间距
#define Button_Height 90.0f    // 高
#define Button_Width ((kWindowW-30)/3.0)     // 宽

@interface ShangChengVC ()<SDCycleScrollViewDelegate>

@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic, strong) NSMutableArray *bannerArr;

@end

@implementation ShangChengVC

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
//    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    [self loadBannerData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    self.viewTitle.text = LocalizationKey(@"商城");
    self.bannerArr = [[NSMutableArray alloc]init];
    
    [self setLunBoView];
}

- (void)loadBannerData{
    [NewAllNetManager kuangJiBanner:@"" CompleteHandle:^(id resPonseObj, int code) {
        if (code) {
            NSInteger codeInt = [resPonseObj[@"code"] intValue];
            if (codeInt == 0) {
                self.bannerArr = [[NSMutableArray alloc]init];
                self.bannerArr = resPonseObj[@"data"][@"banner"];
                
                [self.cycleScrollView removeFromSuperview];
                 self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(16, 20, kWindowW-32, (162.0/375.0)*kWindowW) shouldInfiniteLoop:YES imageNamesGroup:self.bannerArr];
                self.cycleScrollView.layer.masksToBounds = YES;
                self.cycleScrollView.layer.cornerRadius = 6.0;
                self.cycleScrollView.delegate = self;
                self.cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleNone;
                [self.view addSubview:self.cycleScrollView];
                self.cycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
                
            }
        }
    }];
}

- (void)setLunBoView{
   self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(16, 20, kWindowW-32, (162.0/375.0)*kWindowW) shouldInfiniteLoop:YES imageNamesGroup:self.bannerArr];
    self.cycleScrollView.layer.masksToBounds = YES;
    self.cycleScrollView.layer.cornerRadius = 6.0;
    self.cycleScrollView.delegate = self;
    self.cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleNone;
    [self.view addSubview:self.cycleScrollView];
    self.cycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
          //    cycleScrollView.autoScrollTimeInterval = 4.0;
    
//    UIImageView *bannerImage = [[UIImageView alloc]init];
//    [self.cycleScrollView addSubview:bannerImage];
//    [bannerImage mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.top.right.bottom.equalTo(0);
//    }];
//    bannerImage.image = ImageName(@"s_banner");
    
    UIView *headLineView = [[UIView alloc]init];
    [self.view addSubview:headLineView];
    [headLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(0);
        make.top.equalTo((162.0/375.0)*kWindowW+33);
        make.height.equalTo(9);
    }];
    headLineView.backgroundColor = [UIColor colorWithHEX:@"#F5F5F5"];
    
    UIView *titleView = [[UIView alloc]init];
    [self.view addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(0);
        make.top.equalTo((162.0/375.0)*kWindowW+64);
        make.height.equalTo(250);
    }];
    
    UILabel *titleLB = [[UILabel alloc]init];
    [titleView addSubview:titleLB];
    [titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(25);
        make.top.equalTo(0);
    }];
    titleLB.text = LocalizationKey(@"商品分类");
    titleLB.textColor = tMainColor;
    
    UIView *shuView = [[UIView alloc]init];
    [titleView addSubview:shuView];
    [shuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(15);
        make.centerY.equalTo(titleLB);
        make.width.equalTo(2);
        make.height.equalTo(9);
    }];
    shuView.backgroundColor = tMainColor;
    
    NSArray *headArr = @[LocalizationKey(@"矿机"), LocalizationKey(@"进口商品"), LocalizationKey(@"酒店"), LocalizationKey(@"机票"), LocalizationKey(@"加油卡"), LocalizationKey(@"更多")];
    NSArray *headImageArr = @[@"s_kuangji", @"s_jinkoushangpin", @"s_jiudian", @"s_jipiao", @"s_jiayouka", @"s_gengduo"];

       for (int i = 0; i < 6; i ++) {
           NSInteger index = i % 3;
           NSInteger page = i / 3;
           
           SYButton *headBtn = [[SYButton alloc]init];
           [titleView addSubview:headBtn];
           headBtn.frame = CGRectMake(index * (Button_Width + Width_Space) + Start_X, page  * (Button_Height + Height_Space)+Start_Y, Button_Width, Button_Height);
           [headBtn setTitle:headArr[i] forState:UIControlStateNormal];
           [headBtn setImage:ImageName(headImageArr[i]) forState:UIControlStateNormal];
           [headBtn setTitleColor:tThreeColor forState:UIControlStateNormal];
           headBtn.titleLabel.font = kFont(12);
           headBtn.btnType = TOP;
           headBtn.tag = 200 + i;
           [headBtn addTarget:self action:@selector(headBtnAct:) forControlEvents:UIControlEventTouchUpInside];
       }
}

- (void)headBtnAct:(UIButton *)sender{
    if (sender.tag == 200) {
        KuangJiVC *VC = [[KuangJiVC alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    else{
        [self.view makeToast:LocalizationKey(@"敬请期待") duration:1.5 position:CSToastPositionCenter];
    }
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
